package br.com.ldml2;

import br.com.util.Escritor;
import br.com.util.TripleSet;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("filter")
public class Filter implements LdmlCommand {
	@XStreamAsAttribute
	private String dataset, property, equality, object, output;
	
	public String getDataset() {
		return dataset;
	}

	public void setDataset(String dataset) {
		this.dataset = dataset;
	}

	public String getEquality() {
		return equality;
	}

	public void setEquality(String equality) {
		this.equality = equality;
	}

	public String getObject() {
		return object;
	}

	public void setObject(String object) {
		this.object = object;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	@Override
	public void exec(String tmpFileName) {
		TripleSet tsIn = new TripleSet(dataset);
		TripleSet tsOut = new TripleSet();
		
		for(String subject : tsIn.getTripleSet().keySet()) {
			if (tsIn.getTripleSet().get(subject).containsKey(property)) {
				for(String object : tsIn.getTripleSet().get(subject).get(property)) {
					try {
						Integer i = Integer.parseInt(object);
						
						switch (equality) {
						case "<":
							if(Integer.parseInt(this.object) < i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
							break;
						case "<=":
							if(Integer.parseInt(this.object) <= i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
							break;
						case "==":
							if(Integer.parseInt(this.object) == i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
							break;	
						case ">=":
							if(Integer.parseInt(this.object) >= i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
							break;	
						case ">":
							if(Integer.parseInt(this.object) > i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
							break;	
						case "!=":
							if(Integer.parseInt(this.object) != i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
							break;	
						}
					} catch (Exception e) {
						try {
							Double i = Double.parseDouble(object);
							
							switch (equality) {
							case "<":
								if(Double.parseDouble(this.object) < i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;
							case "<=":
								if(Double.parseDouble(this.object) <= i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;
							case "==":
								if(Double.parseDouble(this.object) == i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;	
							case ">=":
								if(Double.parseDouble(this.object) >= i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;	
							case ">":
								if(Double.parseDouble(this.object) > i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;	
							case "!=":
								if(Double.parseDouble(this.object) != i) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;	
							}
						} catch (Exception e1) {
							switch (equality) {
							case "==":
								if(this.object.equals(object)) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;	
							case "!=":
								if(!this.object.equals(object)) tsOut.getTripleSet().put(subject, tsIn.getTripleSet().get(subject));
								break;	
							}
						}
					}
				}
			}
		}
		
		XStream xs2 = new XStream();
		Escritor e = new Escritor();
		e.abreArquivo(tmpFileName, true);
		e.gravaArquivo(new String[] { xs2.toXML(tsOut) } );
		
		e.fechaArquivo();
	}

	@Override
	public String getOutput() {
		return output;
	}

	@Override
	public void setOutput(String output) {
		this.output = output;
	}
}
